/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sys.screen;

import java.io.IOException;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import sys.bean.GrupoSubGrupoBean;
import sys.bean.MovimentacaoBean;
import sys.bean.PedidoBean;
import sys.bean.UsuarioBean;
import sys.dao.GrupoSubGrupoDao;
import sys.dao.MovimentacaoDao;
import sys.dao.PedidoDao;
import sys.dao.UsuarioDao;
import sys.utils.Mensagens;
import sys.utils.Utils;
import static sys.utils.Utils.numParaCalculos;

/**
 *
 * @author Administrador
 */
public class UIVendaGrupoSubGrupo extends javax.swing.JDialog {

    private List<UsuarioBean> listaVendedores;
    private List<GrupoSubGrupoBean> listaGrupos;
    private List<GrupoSubGrupoBean> listaSubGrupos;
    SimpleDateFormat formDataSpac = new SimpleDateFormat("yyyyMMdd");
    NumberFormat formatoMoeda = NumberFormat.getCurrencyInstance();

    String vendedor = "";
    String grupo = "";
    String subGrupo = "";

    public UIVendaGrupoSubGrupo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Utils.escClose(this);
        setLocationRelativeTo(null);
        dataInicial.setDate(new Date());
        dataFinal.setDate(new Date());
        atualizaVendedores();
        atualizaGrupos();
        UIManager.put("Table.alternateRowColor", new java.awt.Color(230, 255, 255));
        Utils.maximizarDialog(this);
        jpTabela.setVisible(false);
    }

    /*
     metodo chamado quando mouse ou tecla passa na tabela.
     Soma o que tem na tabela e exibe o valor
     */
    private void somaTabela() {

        if (jTabbedPane.getSelectedIndex() == 0) {
            double soma = 0;
            int[] linhas = tabelaAnalitico.getSelectedRows();
            for (int i = 0; i < linhas.length; i++) {
                soma += Double.parseDouble(numParaCalculos(tabelaAnalitico.getValueAt(linhas[i], 1).toString()));
            }
            lbSoma.setText(formatoMoeda.format(soma));
        } else {
            double soma = 0;
            int[] linhas = tabelaSintetico.getSelectedRows();

            for (int i = 0; i < linhas.length; i++) {
                soma += Double.parseDouble(numParaCalculos(tabelaSintetico.getValueAt(linhas[i], 7).toString()));
            }
            lbSoma.setText(formatoMoeda.format(soma));
        }
    }

    private void gerarRelatorio() {
        if (jTabbedPane.getSelectedIndex() == 0) {
            jTabbedPane.setSelectedIndex(0);
            jpTabela.setVisible(true);
            atualizaTabelaAnalitico();
            atualizaGrafico();
        } else {
            jTabbedPane.setSelectedIndex(1);
            atualizaTabelaSintetico();
        }

    }

    private void atualizaTabelaSintetico() {
        carregaDados();

        DefaultTableModel modelo = (DefaultTableModel) tabelaSintetico.getModel();
        modelo.setNumRows(0);

        try {
            for (MovimentacaoBean mov : MovimentacaoDao.vendaGrupoSubGrupoSintetico(formDataSpac.format(dataInicial.getDate()), formDataSpac.format(dataFinal.getDate()), vendedor, grupo, subGrupo)) {
                modelo.addRow(new Object[]{
                    mov.getPedido().getData(), mov.getPedido().getPedido(), mov.getUsuario().getNome(), mov.getProduto().getCodigo(), mov.getProduto().getDescricao(),
                    formatoMoeda.format(mov.getQuantidade()).replace("R$", ""), formatoMoeda.format(mov.getValor()).replace("R$", ""),
                    formatoMoeda.format(mov.getQuantidade() * mov.getValor()).replace("R$", "")
                });
            }

            DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();

            centralizado.setHorizontalAlignment(SwingConstants.CENTER);
            tabelaSintetico.getColumnModel().getColumn(0).setCellRenderer(centralizado);
            tabelaSintetico.getColumnModel().getColumn(1).setCellRenderer(centralizado);
            tabelaSintetico.getColumnModel().getColumn(3).setCellRenderer(centralizado);
            tabelaSintetico.getColumnModel().getColumn(5).setCellRenderer(centralizado);
            tabelaSintetico.getColumnModel().getColumn(6).setCellRenderer(centralizado);
            tabelaSintetico.getColumnModel().getColumn(7).setCellRenderer(centralizado);

        } catch (SQLException | IOException ex) {
            Mensagens.msgErro("erro ao atualizar tabela\n" + ex.getMessage());
        }

    }

    private void atualizaTabelaAnalitico() {
        carregaDados();
        DefaultTableModel modelo = (DefaultTableModel) tabelaAnalitico.getModel();
        modelo.setNumRows(0);

        try {
            for (PedidoBean pedido : PedidoDao.vendaGrupoSubGrupoAnalitico(formDataSpac.format(dataInicial.getDate()), formDataSpac.format(dataFinal.getDate()), vendedor, grupo, subGrupo)) {
                modelo.addRow(new Object[]{
                    pedido.getUsuario().getNome(), formatoMoeda.format(pedido.getvFinal()).replace("R$", "")
                });
            }
            DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();

            centralizado.setHorizontalAlignment(SwingConstants.CENTER);
            tabelaAnalitico.getColumnModel().getColumn(1).setCellRenderer(centralizado);

        } catch (SQLException | IOException ex) {
            Mensagens.msgErro("erro\n" + ex.getMessage());
        }
    }

    /*
     * Gerando o grafico de vendas (barra)
     *
     */
    private void atualizaGrafico() {

        CategoryDataset cds = cdtVendasGrupoSubGrupo();

        String titulo = "VENDA GRUPO/SUBGRUPO";
        String eixoY = "INTERVALO DE VALORES";
        String txLegenda = "";
        boolean legenda = true;
        boolean tooltips = false;
        boolean urls = false;

        JFreeChart graf = ChartFactory.createBarChart("", txLegenda, "", cds, PlotOrientation.VERTICAL, legenda, tooltips, urls);
        CategoryPlot plot = (CategoryPlot) graf.getCategoryPlot();

        CategoryItemRenderer itemRerender = plot.getRenderer();

        itemRerender.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", NumberFormat.getCurrencyInstance()));
        itemRerender.setItemLabelsVisible(true);

        ChartPanel myChartpanel = new ChartPanel(graf, true);
        myChartpanel.setSize(jpGrafico.getWidth(), jpGrafico.getHeight());
        myChartpanel.setVisible(true);
        jpGrafico.removeAll();
        jpGrafico.add(myChartpanel);
        jpGrafico.revalidate();
        jpGrafico.repaint();
    }

    /*
     * Carregando dados para gerar o grafico de vendas(barra)
     *
     */
    private CategoryDataset cdtVendasGrupoSubGrupo() {

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        carregaDados();
        try {
            for (PedidoBean pedido : PedidoDao.vendaGrupoSubGrupoAnalitico(formDataSpac.format(dataInicial.getDate()), formDataSpac.format(dataFinal.getDate()), vendedor, grupo, subGrupo)) {
                dataset.addValue(pedido.getvFinal(), pedido.getUsuario().getNome(), "");
            }
        } catch (IOException | SQLException ex) {
            Mensagens.msgErro("Erro\n" + ex.getMessage());
        }
        return dataset;
    }

    /*
     carregando dados, vendedor, grupo e subgrupo
     */
    private void carregaDados() {
        if (cbVendedor.getSelectedIndex() == 0) {
            vendedor = "";
        } else {
            vendedor = listaVendedores.get(cbVendedor.getSelectedIndex() - 1).getCodigo();
        }

        if (cbGrupo.getSelectedIndex() == 0) {
            grupo = "";
        } else {
            grupo = listaGrupos.get(cbGrupo.getSelectedIndex() - 1).getGrupo();
        }

        if (cbSubGrupo.getSelectedIndex() == 0) {
            subGrupo = "";
        } else {
            subGrupo = listaSubGrupos.get(cbSubGrupo.getSelectedIndex() - 1).getSubGrupo();
        }

    }

    private void atualizaVendedores() {
        try {
            cbVendedor.removeAllItems();
            cbVendedor.addItem("<< TODOS OS VENDEDORES >>");
            listaVendedores = UsuarioDao.getListaComboVendedores();

            for (UsuarioBean usu : listaVendedores) {
                cbVendedor.addItem(usu.getCodigo() + " - " + usu.getNome());
            }
        } catch (IOException | SQLException ex) {
            Mensagens.msgErro("erro\n" + ex.getMessage());
        }
    }

    private void atualizaGrupos() {
        try {
            cbGrupo.removeAllItems();
            cbGrupo.addItem("<< TODOS OS GRUPOS >>");
            listaGrupos = GrupoSubGrupoDao.listaGrupos();
            for (GrupoSubGrupoBean grupoB : listaGrupos) {
                cbGrupo.addItem(grupoB.getGrupo() + " - " + grupoB.getDescricao());
            }
        } catch (IOException | SQLException ex) {
            Mensagens.msgErro("erro\n" + ex.getMessage());
        }
    }

    private void atualizaSubGrupos() {
        try {
            cbSubGrupo.removeAllItems();
            cbSubGrupo.addItem("<< TODOS OS SUBGRUPOS >>");
            if (cbGrupo.getSelectedIndex() == 0) {
                cbSubGrupo.removeAllItems();
                cbSubGrupo.addItem("<< TODOS OS SUBGRUPOS >>");
            } else {
                listaSubGrupos = GrupoSubGrupoDao.listaSubGrupos(listaGrupos.get(cbGrupo.getSelectedIndex() - 1).getGrupo());

                for (GrupoSubGrupoBean grupo : listaSubGrupos) {
                    cbSubGrupo.addItem(grupo.getSubGrupo() + " - " + grupo.getDescricao());
                }
            }
        } catch (IOException | SQLException ex) {
            Mensagens.msgErro("erro\n" + ex.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jpDados = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        cbVendedor = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        dataInicial = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/#####", '_');
        jLabel6 = new javax.swing.JLabel();
        dataFinal = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/#####", '_');
        cbGrupo = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        cbSubGrupo = new javax.swing.JComboBox();
        lbSoma = new javax.swing.JLabel();
        jTabbedPane = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jpGrafico = new javax.swing.JPanel();
        jpTabela = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAnalitico = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaSintetico = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        btnSalvar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("VENDA DE PRODUTOS GRUPO/SUBGRUPO");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jpDados.setBackground(new java.awt.Color(255, 255, 255));
        jpDados.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "SELECIONE DADOS:", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Calibri", 0, 10))); // NOI18N

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel5.setText("VENDEDOR:");

        cbVendedor.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbVendedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< TODOS OS VENDEDORES >>" }));
        cbVendedor.setPreferredSize(new java.awt.Dimension(185, 22));

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel2.setText("DATA INICIAL:");

        dataInicial.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel6.setText("DATA FINAL:");

        dataFinal.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        cbGrupo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbGrupo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< TODOS OS GRUPOS >>" }));
        cbGrupo.setPreferredSize(new java.awt.Dimension(185, 22));
        cbGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbGrupoActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel7.setText("GRUPO:");

        jLabel8.setFont(new java.awt.Font("Calibri", 0, 11)); // NOI18N
        jLabel8.setText("SUBGRUPO:");

        cbSubGrupo.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbSubGrupo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<< TODOS OS SUBGRUPOS >>" }));
        cbSubGrupo.setPreferredSize(new java.awt.Dimension(185, 22));

        lbSoma.setFont(new java.awt.Font("Calibri", 1, 13)); // NOI18N
        lbSoma.setForeground(new java.awt.Color(102, 0, 102));
        lbSoma.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jpDadosLayout = new javax.swing.GroupLayout(jpDados);
        jpDados.setLayout(jpDadosLayout);
        jpDadosLayout.setHorizontalGroup(
            jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDadosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(dataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(dataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(cbVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(cbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jpDadosLayout.createSequentialGroup()
                        .addComponent(cbSubGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbSoma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jpDadosLayout.setVerticalGroup(
            jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpDadosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpDadosLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(23, 23, 23))
                    .addComponent(cbGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpDadosLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(cbVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpDadosLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(1, 1, 1)
                        .addComponent(dataFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpDadosLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(1, 1, 1)
                        .addComponent(dataInicial, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpDadosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(lbSoma, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cbSubGrupo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jTabbedPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPaneMouseClicked(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jpGrafico.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpGraficoLayout = new javax.swing.GroupLayout(jpGrafico);
        jpGrafico.setLayout(jpGraficoLayout);
        jpGraficoLayout.setHorizontalGroup(
            jpGraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 726, Short.MAX_VALUE)
        );
        jpGraficoLayout.setVerticalGroup(
            jpGraficoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 362, Short.MAX_VALUE)
        );

        tabelaAnalitico.setAutoCreateRowSorter(true);
        tabelaAnalitico.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tabelaAnalitico.setForeground(new java.awt.Color(51, 102, 255));
        tabelaAnalitico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "VENDEDOR", "TOTAL BRUTO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaAnalitico.setGridColor(new java.awt.Color(204, 204, 204));
        tabelaAnalitico.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tabelaAnaliticoMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabelaAnalitico);
        if (tabelaAnalitico.getColumnModel().getColumnCount() > 0) {
            tabelaAnalitico.getColumnModel().getColumn(0).setPreferredWidth(150);
        }

        javax.swing.GroupLayout jpTabelaLayout = new javax.swing.GroupLayout(jpTabela);
        jpTabela.setLayout(jpTabelaLayout);
        jpTabelaLayout.setHorizontalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
        );
        jpTabelaLayout.setVerticalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpGrafico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(15, 15, 15))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jpGrafico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(13, 13, 13))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane.addTab("ANALÍTICO", jPanel2);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));

        tabelaSintetico.setAutoCreateRowSorter(true);
        tabelaSintetico.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        tabelaSintetico.setForeground(new java.awt.Color(51, 102, 255));
        tabelaSintetico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "DATA", "PEDIDO", "VENDEDOR", "CODIGO", "PRODUTO", "QTD", "VR. UNT", "TOTAL"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaSintetico.setGridColor(new java.awt.Color(204, 204, 204));
        tabelaSintetico.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tabelaSinteticoMouseReleased(evt);
            }
        });
        tabelaSintetico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaSinteticoKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tabelaSintetico);
        if (tabelaSintetico.getColumnModel().getColumnCount() > 0) {
            tabelaSintetico.getColumnModel().getColumn(3).setPreferredWidth(20);
            tabelaSintetico.getColumnModel().getColumn(4).setPreferredWidth(300);
            tabelaSintetico.getColumnModel().getColumn(5).setPreferredWidth(20);
        }

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1010, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        jTabbedPane.addTab("SINTÉTICO", jPanel3);

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));

        btnSalvar.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/find-2.png"))); // NOI18N
        btnSalvar.setText("GERAR");
        btnSalvar.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jPanel4.add(btnSalvar);

        btnSair.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sys/picture/sair.png"))); // NOI18N
        btnSair.setText("SAIR");
        btnSair.setPreferredSize(new java.awt.Dimension(120, 30));
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jPanel4.add(btnSair);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpDados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTabbedPane, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpDados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbGrupoActionPerformed
        try {
            atualizaSubGrupos();
        } catch (NullPointerException e) {
            //
        }
    }//GEN-LAST:event_cbGrupoActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        final BarraProgresso pb = new BarraProgresso(this);
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                pb.gerarJanela();
                pb.lbDescricao.setText("CARREGANDO, POR FAVOR AGUARDE...");
                gerarRelatorio();
                return null;
            }

            @Override
            protected void done() {
                pb.fechar();
            }
        };
        worker.execute();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void tabelaAnaliticoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaAnaliticoMouseReleased
        somaTabela();
    }//GEN-LAST:event_tabelaAnaliticoMouseReleased

    private void jTabbedPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPaneMouseClicked
        lbSoma.setText("");
    }//GEN-LAST:event_jTabbedPaneMouseClicked

    private void tabelaSinteticoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaSinteticoMouseReleased
        somaTabela();
    }//GEN-LAST:event_tabelaSinteticoMouseReleased

    private void tabelaSinteticoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaSinteticoKeyReleased
        somaTabela();
    }//GEN-LAST:event_tabelaSinteticoKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cbGrupo;
    private javax.swing.JComboBox cbSubGrupo;
    private javax.swing.JComboBox cbVendedor;
    private com.toedter.calendar.JDateChooser dataFinal;
    private com.toedter.calendar.JDateChooser dataInicial;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane;
    private javax.swing.JPanel jpDados;
    private javax.swing.JPanel jpGrafico;
    private javax.swing.JPanel jpTabela;
    private javax.swing.JLabel lbSoma;
    private javax.swing.JTable tabelaAnalitico;
    private javax.swing.JTable tabelaSintetico;
    // End of variables declaration//GEN-END:variables
}
